<template>
    <div>
        <h3 class="bolder red center" style="font-family: 'Courier New', Courier, monospace">
            <i class="ace-icon fa fa-hand-o-right icon-animated-hand-pointer blue"></i>
            分类列表:<a href="#" class="pink"> {{currentParent.name}}</a>
        </h3>
        <p>

            <button class="btn btn-white btn-default btn-round" @click="refresh()">
                <i class="ace-icon fa fa-refresh red"></i>
                刷新
            </button>
            &nbsp;
            <button class="btn btn-white btn-success btn-round" @click="openAddModal()">
                <i class="ace-icon fa fa-edit blue"></i>
                新增
            </button>
			<button v-show="currentParent.id !== root.id" class="btn btn-white btn-default btn-round" @click="returnLast()">
				<i class="ace-icon fa fa-arrow-left blue"></i>
				上一级
			</button>
        </p>


        <table id="simple-table" class="table  table-bordered table-hover">
            <thead>
            <tr>
                <th class="center">ID</th>
                <th class="center">名称</th>
                <th class="center">排序</th>
                <th class="center">操作</th>
            </tr>
            </thead>

            <tbody>
            <tr v-for="category in categorys">
                <td class="center">{{category.id}}</td>
                <td class="center">{{category.name}}</td>
                <td class="center">{{category.sort}}</td>
                <td class="center" style="width:150px;">
                    <div class=" btn-group">
                        <button v-on:click="list(category)" class="btn btn-white btn-xs btn-info btn-round">
                            下级
                        </button>
                        <button v-on:click="openEditModal(category.id)" class="btn btn-white btn-xs btn-info btn-round">
                            <i class="ace-icon fa fa-edit bigger-120"></i>
                        </button>
                        &nbsp;&nbsp;&nbsp;
                        <button v-on:click="deleteById(category.id)" class="btn btn-white btn-xs btn-danger btn-round">
                            <i class="ace-icon fa fa-remove bigger-120"></i>
                        </button>
                    </div>

                </td>
            </tr>
            <tr v-if="categorys.length === 0">
                <td colspan="5" class="center" style="background-color: lightgrey;font-family: 'Courier New',serif">
                    暂无数据
                </td>
            </tr>
            </tbody>
        </table>

        <!-- 模态框 -->
        <div class="modal fade" id="myModal1" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
            <div class="modal-dialog" role="document">
                <div class="modal-content" style="border-color: #aa160e;background-color: lightblue">
                    <div class="modal-header">
                        <button type="button" class="close blue" data-dismiss="modal" aria-label="Close"><span
                                aria-hidden="true">&times;</span></button>
                        <h4 class="modal-title grey bolder" style="font-family: 'Courier New', Courier, monospace"
                            id="myModalLabel">{{modalTitle}}</h4>
                    </div>
                    <div class="modal-body">
                        <form class="form-horizontal" role="form">
                            <div class="form-group">
                                <label class="col-sm-3 control-label no-padding-right"
                                       style="font-family: 'Courier New', Courier, monospace"> 名称 </label>

                                <div class="col-sm-9">
                                    <input v-model="category.name" type="text" placeholder="分类名称"
                                           class="col-xs-10 col-sm-5"/>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-3 control-label no-padding-right"
                                       style="font-family: 'Courier New', Courier, monospace"> 父目录</label>

                                <div class="col-sm-9">
                                    <input disabled v-model="currentParent.name" type="text" placeholder="根"
                                           class="col-xs-10 col-sm-5"/>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-3 control-label no-padding-right"
                                       style="font-family: 'Courier New', Courier, monospace">排序 </label>

                                <div class="col-sm-9">
                                    <input v-model="category.sort" type="text" placeholder="排序"
                                           class="col-xs-10 col-sm-5"/>
                                </div>
                            </div>
                        </form>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default btn-round"
                                style="font-family: 'Courier New', Courier, monospace" data-dismiss="modal">
                            关闭
                        </button>
                        <button @click="save()" type="button" class="btn btn-primary btn-round"
                                style="font-family: 'Courier New', Courier, monospace">
                            保存
                        </button>
                    </div>
                </div>
            </div>
        </div>
    </div>

</template>

<script>
    export default {
        name: "category",
        components: {},
        data: function () {
            return {
                categorys: [],
                category: {},
                modalTitleArr: {
                    insert: "新增分类",
                    update: "修改分类"
                },
                modalTitle: "",
                root: {"id": "00000000", "name": "根目录"},
                currentParent: {"id": "00000000", "name": "根目录"}
            }
        },
        mounted: function () {
            let _this = this;
            //初始化默认是根目录
            _this.list(_this.root);
        },
        methods: {
        	/*返回上一级*/
			returnLast(){
				let _this = this;
				//如果上一级的父节点是根节点
				if (_this.currentParent.parent===this.root.id) {
					_this.list(_this.root);
					return;
				}
				//查找上一级的父节点
				_this.$ajax.get(process.env.VUE_APP_SERVER +
						'/business/admin/category/findById/' +
						_this.currentParent.parent).then((response) => {
					_this.currentParent = response.data.data;
					_this.list(_this.currentParent);
				})
			},
            /* 打开新增模态框 */
            openAddModal() {
                let _this = this;
                _this.clearCategory();
                _this.modalTitle = _this.modalTitleArr.insert;
                $("#myModal1").modal({backdrop: 'static'});
                $("#myModal1").modal("show");

            },
            /*打开编辑模态框*/
            openEditModal(id) {
                let _this = this;
                _this.clearCategory();
                _this.modalTitle = _this.modalTitleArr.update;
                _this.$ajax.get(process.env.VUE_APP_SERVER + '/business/admin/category/findById/' + id).then((response) => {
                    _this.category = response.data.data;
                });
                $("#myModal1").modal({backdrop: 'static'});
                $("#myModal1").modal("show");
            },
            refresh() {
                let _this = this;
                Loading.show();
                //根据父节点刷新
                _this.list(_this.currentParent);
                Loading.hide();
            },
            /* 查找分类 */
            list(parent) {
                let _this = this;
                _this.currentParent = parent;
                _this.$ajax.post(process.env.VUE_APP_SERVER + '/business/admin/category/list?parent=' + parent.id).then((response) => {
                    _this.categorys = response.data.data;
                })
            },
            /*保存分类信息到后台*/
            save() {
                let _this = this;
                _this.category.parent = _this.currentParent.id;
                if (!_this.category.id) {
                    /*不存在id为新增*/
                    _this.insert();
                } else {
                    /*存在id为修改*/
                    _this.update();
                }
            },
            /*新增分类*/
            insert() {
                let _this = this;
                /*新增校验*/
                // if (!Validator.require(_this.category.name,"名称")
                // || !Validator.require(_this.category.courseId, "课程")
                // || !Validator.length(_this.category.courseId,"课程ID",1,19)) {
                // 	return;
                // }
                _this.$ajax.post(process.env.VUE_APP_SERVER + '/business/admin/category/save', _this.category).then((response) => {
                    console.log(response);
                    if (response.data.success) {
                        _this.list(_this.currentParent);
                        Toast.success(response.data.message);
                    } else {
                        Toast.error(response.data.message);
                    }
                    $("#myModal1").modal("hide");
                });
            },
            /*修改分类*/
            update() {
                let _this = this;
                /*更新校验*/
                /*if (!Validator.require(_this.category.name,"名称")
                        && !Validator.require(_this.category.courseId, "课程")
                        || !Validator.length(_this.category.courseId,"课程ID",1,19)) {
                    return;
                }*/
                _this.$ajax.put(process.env.VUE_APP_SERVER + '/business/admin/category/update', _this.category).then((response) => {
                    if (response.data.success) {
                        _this.list(_this.currentParent);
                        Toast.success(response.data.message);
                    } else {
                        Toast.error(response.data.message);
                    }
                    $("#myModal1").modal("hide");
                })
            },
            /*删除分类*/
            deleteById(id) {
                let _this = this;
                /*删除提示框*/
                Confirm.show("", () => {
                    _this.$ajax.delete(process.env.VUE_APP_SERVER + '/business/admin/category/deleteById/' + id).then((response) => {
                        if (response.data.success) {
                            _this.list(_this.currentParent);
                            Toast.success(response.data.message);
                        } else {
                            Toast.error(response.data.message);
                        }
                        $("#myModal1").modal("hide");
                    })
                });
            },
            /*清空模态框的数据*/
            clearCategory() {
                let _this = this;
                _this.category = {};
                _this.modalTitle = "";
            }
        }
    }
</script>
